#include "datasum.h"
#include "ui_datasum.h"

datasum::datasum(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::datasum)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(),this->height());
    ConnectDatabase();
}

datasum::~datasum()
{
    delete ui;
}
void datasum::ConnectDatabase()
{
    QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL");
    database.setHostName("localhost");
    database.setPort(3306);
    database.setPassword("root");
    database.setUserName("root");
    database.setDatabaseName("stockdatabase");
    if(!database.open())
    {
        QMessageBox::information(this,"提示","数据库连接失败！！",QMessageBox::Yes);
    }
}

void datasum::QueryDataSumFunc(QSqlQuery sql)
{
    int count = 0;
    while(sql.next())
    {

        QString id = sql.value(0).toString();
        QString name = sql.value(1).toString();
        QString amount = sql.value(2).toString();
        QString price = sql.value(3).toString();
        QString supplier = sql.value(4).toString();
        QString director = sql.value(5).toString();
        QString warehoustime = sql.value(6).toString();
        QString deliverytime = sql.value(7).toString();
        QString remarks = sql.value(8).toString();
        double sumprice = amount.toDouble()*price.toDouble();
        QString sum = QString("%1").arg(sumprice);

        ui->tableWidget->setItem(count,0,new QTableWidgetItem(id));
        ui->tableWidget->setItem(count,1,new QTableWidgetItem(name));
        ui->tableWidget->setItem(count,2,new QTableWidgetItem(amount));
        ui->tableWidget->setItem(count,3,new QTableWidgetItem(price));
        ui->tableWidget->setItem(count,4,new QTableWidgetItem(sum));
        ui->tableWidget->setItem(count,5,new QTableWidgetItem(supplier));
        ui->tableWidget->setItem(count,6,new QTableWidgetItem(director));
        ui->tableWidget->setItem(count,7,new QTableWidgetItem(warehoustime));
        ui->tableWidget->setItem(count,8,new QTableWidgetItem(deliverytime));
        ui->tableWidget->setItem(count,9,new QTableWidgetItem(remarks));
        count++;
    }
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
//商品汇总
void datasum::on_pushButtonGooodsSum_clicked()
{
    QSqlQuery sqlQuery;
    sqlQuery.prepare("SELECT * FROM stock_info;");
    if(!sqlQuery.exec())
    {
        qDebug() << "执行SQL查询时出错:" << sqlQuery.lastError().text();
        return;
    }
    int columu = 10;
    int row = sqlQuery.size();
    ui->tableWidget->setColumnCount(columu);
    ui->tableWidget->setRowCount(row);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"编号"<<"名称"<<"数量"<<"单价"<<"总价"<<"供应商家"<<"负责人"<<"入库时间"<<"出库时间"<<"备注");
    QueryDataSumFunc(sqlQuery);
}
//入库汇总
void datasum::on_pushButtonWarehousSum_clicked()
{
    QSqlQuery sqlQuery;
    sqlQuery.prepare("SELECT * FROM inputdata;");
    if(!sqlQuery.exec())
    {
        qDebug() << "执行SQL查询时出错:" << sqlQuery.lastError().text();
        return;
    }
    int columu = 10;
    int row = sqlQuery.size();
    ui->tableWidget->setColumnCount(columu);
    ui->tableWidget->setRowCount(row);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"序号"<<"商品编号"<<"商品名称"<<"入库数量"<<"单价"<<"供应商家"<<"负责人"<<"入库时间"<<"备注");
    int count = 0;
    while(sqlQuery.next())
    {

        QString id = sqlQuery.value(0).toString();
        QString goodsid = sqlQuery.value(1).toString();
        QString name = sqlQuery.value(2).toString();
        QString warehousamount = sqlQuery.value(3).toString();
        QString price = sqlQuery.value(4).toString();
        QString supplier = sqlQuery.value(5).toString();
        QString director = sqlQuery.value(6).toString();
        QString warehoustime = sqlQuery.value(7).toString();

        QString remarks = sqlQuery.value(8).toString();



        ui->tableWidget->setItem(count,0,new QTableWidgetItem(id));
        ui->tableWidget->setItem(count,1,new QTableWidgetItem(goodsid));
        ui->tableWidget->setItem(count,2,new QTableWidgetItem(name));
        ui->tableWidget->setItem(count,3,new QTableWidgetItem(warehousamount));
        ui->tableWidget->setItem(count,5,new QTableWidgetItem(price));
        ui->tableWidget->setItem(count,6,new QTableWidgetItem(supplier));
        ui->tableWidget->setItem(count,7,new QTableWidgetItem(director));
        ui->tableWidget->setItem(count,8,new QTableWidgetItem(warehoustime));
        ui->tableWidget->setItem(count,9,new QTableWidgetItem(remarks));
        count++;
    }
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

}
//出库汇总
void datasum::on_pushButtonDeliverySum_clicked()
{
    QSqlQuery sqlQuery;
    sqlQuery.prepare("SELECT * FROM outputdata;");
    if(!sqlQuery.exec())
    {
        qDebug() << "执行SQL查询时出错:" << sqlQuery.lastError().text();
        return;
    }
    int columu = 10;
    int row = sqlQuery.size();
    ui->tableWidget->setColumnCount(columu);
    ui->tableWidget->setRowCount(row);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"序号"<<"商品编号"<<"商品名称"<<"出库数量"<<"单价"<<"供应商家"<<"负责人"<<"出库时间"<<"备注");
    int count = 0;
    while(sqlQuery.next())
    {

        QString id = sqlQuery.value(0).toString();
        QString goodsid = sqlQuery.value(1).toString();
        QString name = sqlQuery.value(2).toString();
        QString warehousamount = sqlQuery.value(3).toString();
        QString price = sqlQuery.value(4).toString();
        QString supplier = sqlQuery.value(5).toString();
        QString director = sqlQuery.value(6).toString();
        QString delivery = sqlQuery.value(7).toString();

        QString remarks = sqlQuery.value(8).toString();



        ui->tableWidget->setItem(count,0,new QTableWidgetItem(id));
        ui->tableWidget->setItem(count,1,new QTableWidgetItem(goodsid));
        ui->tableWidget->setItem(count,2,new QTableWidgetItem(name));
        ui->tableWidget->setItem(count,3,new QTableWidgetItem(warehousamount));
        ui->tableWidget->setItem(count,5,new QTableWidgetItem(price));
        ui->tableWidget->setItem(count,6,new QTableWidgetItem(supplier));
        ui->tableWidget->setItem(count,7,new QTableWidgetItem(director));
        ui->tableWidget->setItem(count,8,new QTableWidgetItem(delivery));
        ui->tableWidget->setItem(count,9,new QTableWidgetItem(remarks));
        count++;
    }
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
void datasum::on_pushButtonImportData_clicked()
{

}
